import * as olProj from 'ol/proj' // 坐标变换使用
import * as turf from '@turf/turf'
import { transform } from 'ol/proj' // 坐标转换, getTransform
import * as olExtent from 'ol/extent' // 范围信息
import { getDistance } from 'ol/sphere'

export default {
  data () {
    return {
      modOldStyle: {} // 保存修改的feature原来的样式{featureid: oldStyle}
    }
  },
  watch: {
    // watch
  },
  mounted () {
    // mounted
  },
  methods: {

    /**
     * @description 程序休眠
     * @param ms {number}休眠时间，单位是毫秒
     * @return
     */
    sleep (ms) {
      return new Promise(resolve => setTimeout(resolve, ms))
    },

    /**
     * @description 获取当前的视图中心点坐标及当前层级
     * @param null
     * @return
     */
    getCurrentView () {
      const oView = this.imap.getView()
      const sMapRCS = oView.getProjection().getCode()
      let aCenter = oView.getCenter()
      const nZoom = oView.getZoom()
      if (sMapRCS !== 'EPSG:4326') {
        aCenter = olProj.transform(aCenter, sMapRCS, 'EPSG:4326')
      }
      console.log({ cp: aCenter, lv: nZoom })
      return { cp: aCenter, lv: nZoom }
    },

    /**
     * @description 根据图层的名称检查是否存在
     * @param sLayerName {Sring}图层名称；oMap {Object}地图对象
     * @return （Boolean）存在返回true，否则返回false
     */
    hasLayer (sLayerName, oMap) {
      // let oLayer
      let bHasLayer = false
      oMap.getLayers().forEach(function (lyr) {
        if (sLayerName === lyr.get('name')) {
          bHasLayer = true
        }
      })
      return bHasLayer
    },

    /**
     * @description 根据图层的名称获取图层
     * @param sLayerName {Sring}图层名称；oMap {Object}地图对象
     * @return
     */
    getLayerByName (sLayerName, oMap) {
      let oLayer
      oMap.getLayers().forEach(function (lyr) {
        // console.log(lyr.get('name'))
        if (sLayerName === lyr.get('name')) {
          oLayer = lyr
          // return oLayer
        }
      })
      return oLayer
    },

    /**
     * @description 获取给定坐标数据的最大和最小值
     * @param oFeature {Object} 要获取重心点的feature对象
     * @return {Array} 返回[minx, miny, maxx, maxy] 范围盒
     */
    getMinMax (aGeoCoord) {
      const coordArray = aGeoCoord[0]
      // console.log(coordArray.length)
      const iNum = coordArray.length // 边界坐标数组
      if (iNum === 0) return null
      const mixArr = [coordArray[0][0], coordArray[0][1]] // 坐标最小值
      const maxArr = [coordArray[0][0], coordArray[0][1]] // 坐标最大值
      for (let i = 1; i < iNum; i++) {
        const currVal = coordArray[i]
        // 当前值的x小于最小值x
        if (currVal[0] < mixArr[0]) {
          mixArr[0] = currVal[0]
        }
        if (currVal[1] < mixArr[1]) {
          mixArr[1] = currVal[1]
        }
        // 当前值大于最大值
        if (currVal[0] > maxArr[0]) {
          maxArr[0] = currVal[0]
        }
        if (currVal[1] > maxArr[1]) {
          maxArr[1] = currVal[1]
        }
      }
      // 返回 [minx, miny, maxx, maxy] 范围盒
      return [mixArr[0], mixArr[1], maxArr[0], maxArr[1]]
    },

    // 设置视角Polygon MultiPolygon
    async setView (sRegionName) {
      console.log(sRegionName)
      
      const regionData = await this.getRegionDataByName(sRegionName) // getRegionData
      const FeatureData = regionData.data
      // console.log(FeatureData)
      let iBbox = null // [minx, miny, maxx, maxy]
      if (FeatureData.type === 'FeatureCollection') {
        const featureArr = FeatureData.features
        // console.log(featureArr)
        for (let i = 0; i < featureArr.length; i++) {
          const oFeatrue = featureArr[i]
          const geoType = oFeatrue.geometry.type
          if (geoType === 'MultiPolygon') { // 由多个多边形组成
            // const geoCoord = oFeatrue.geometry.coordinates
            // console.log(geoType, geoCoord)
            const polyList = oFeatrue.geometry.coordinates
            for (let j = 0; j < polyList.length; j++) {
              // console.log(polyList[j])
              const tmpBbox = this.getMinMax(polyList[j])
              if (iBbox === null) {
                iBbox = tmpBbox
              } else {
                if (iBbox[0] > tmpBbox[0]) {
                  iBbox[0] = tmpBbox[0]
                }
                if (iBbox[1] > tmpBbox[1]) {
                  iBbox[1] = tmpBbox[1]
                }
                if (iBbox[2] < tmpBbox[2]) {
                  iBbox[2] = tmpBbox[2]
                }
                if (iBbox[3] < tmpBbox[3]) {
                  iBbox[3] = tmpBbox[3]
                }
              }
            }
          }
          if (geoType === 'Polygon') { // 由一个多边形组成
            // const geoCoord = oFeatrue.geometry.coordinates
            // console.log(geoType, geoCoord)
            const polyCoord = oFeatrue.geometry.coordinates
            const tmpBbox = this.getMinMax(polyCoord)
            if (iBbox === null) {
              iBbox = tmpBbox
            } else {
              if (iBbox[0] > tmpBbox[0]) {
                iBbox[0] = tmpBbox[0]
              }
              if (iBbox[1] > tmpBbox[1]) {
                iBbox[1] = tmpBbox[1]
              }
              if (iBbox[2] < tmpBbox[2]) {
                iBbox[2] = tmpBbox[2]
              }
              if (iBbox[3] < tmpBbox[3]) {
                iBbox[3] = tmpBbox[3]
              }
            }
          }
        }
      }
      // console.log(iBbox)
      // 需要坐标转换
      const minCoord = transform([iBbox[0], iBbox[1]], 'EPSG:4326', 'EPSG:3857')
      const maxCoord = transform([iBbox[2], iBbox[3]], 'EPSG:4326', 'EPSG:3857')
      const extent = [minCoord[0], minCoord[1], maxCoord[0], maxCoord[1]]
      // 视角调整
      const iView = this.imap.getView() // 获取当前地图的view
      const iSize = this.imap.getSize() // 获取地图的尺寸
      const resolution = iView.getResolutionForExtent(extent, iSize) //  extent以提供范围和尺寸，获取分辨率
      var center = olExtent.getCenter(extent)
      iView.animate({
        resolution: resolution, // 动画结束时视图的分辨率。如果zoom还提供，则将忽略此选项。
        center: center // 动画结束时视图的中心
        // duration: this.duration_,  // 动画的持续时间（以毫秒为单位）
        // easing: ol.easing.easeOut  // 动画期间使用的缓动功能
      })
    },
    /**
     * @description 获取坐标列表中心点
     * @param {Array} aCoordList 一个坐标数组 [(x,y),(x,y)...]
     * @param {String} sRCS 使用的坐标系，默认'EPSG:4326'
     * @param {boolean} bScreenCoord 是否转成屏幕坐标
     * @return {Array} 返回中心点坐标
     */
    // 获取序列点中心，转成当前地图坐标
    getCoordListCP (aCoordList, sRCS = 'EPSG:4326', bScreenCoord = true) {
      const iNum = aCoordList.length
      if (iNum < 2) return [0, 0]
      let xMax = aCoordList[0][0] // x
      let xMin = aCoordList[0][0]
      let yMax = aCoordList[0][1] // y
      let yMin = aCoordList[0][1]
      for (let i = 1; i < iNum; i++) {
        const aPos = aCoordList[i]
        if (aPos[0] > xMax) xMax = aPos[0] // x
        if (aPos[0] < xMin) xMin = aPos[0]
        if (aPos[1] > yMax) yMax = aPos[1] // y
        if (aPos[1] < yMin) yMin = aPos[1]
      }
      const cpx = (xMin + xMax) / 2
      const cpy = (yMin + yMax) / 2
      // 获取当前地图使用的坐标系
      const oView = this.imap.getView()
      const sMapRCS = oView.getProjection().getCode()
      let CenterPoint
      if (sRCS !== sMapRCS) { // 数据和地图使用不同坐标系
        // console.log(olProj.transform([cpx, cpy], sRCS, sMapRCS))
        CenterPoint = olProj.transform([cpx, cpy], sRCS, sMapRCS)
      } else {
        CenterPoint = [cpx, cpy]
      }
      // 是否转换成屏幕坐标
      if (bScreenCoord) {
        // console.log(CenterPoint, this.imap.getPixelFromCoordinate(CenterPoint))
        return this.imap.getPixelFromCoordinate(CenterPoint)
      } else {
        return CenterPoint
      }
    },
    /**
     * @description 获取范围的中心点（只获取中心点）；ol有相关方法
     * @param oFeature {Object} 要获取重心点的feature对象
     * @return {Array} 返回中心点坐标
     */
    getFeatureCP (oFeature) {
      // let aCenterPoint
      let boundingBox = null
      const iGeometry = oFeature.getGeometry()
      const geoType = iGeometry.getType().toLowerCase()

      if (geoType.toLowerCase() === 'polygon') {
        // console.log(iGeometry.getCoordinates())
        boundingBox = this.getMinMax(iGeometry.getCoordinates())
      }
      if (geoType.toLowerCase() === 'multipolygon') {
        // console.log(iGeometry.getCoordinates()[0])
        const paraArr = iGeometry.getCoordinates()[0]
        boundingBox = this.getMinMax(paraArr)
      }
      if (boundingBox) {
        return [(boundingBox[0] + boundingBox[2]) / 2, (boundingBox[1] + boundingBox[3]) / 2]
      } else {
        return null
      }
    },

    /**
     * @description 根据feature获取中心点和地图等级
     * @param {Object} oFeature 要获取重心点的feature对象
     * @return {Object} 返回{cp: 中心点坐标, lv: 显示级别}
     */
    getFeatureCPLv (oFeature, oMap) {
      let aCenterPoint = null
      let boundingBox = null
      const iGeometry = oFeature.getGeometry()
      const geoType = iGeometry.getType().toLowerCase()

      if (geoType.toLowerCase() === 'polygon') {
        // console.log(iGeometry.getCoordinates())
        boundingBox = this.getMinMax(iGeometry.getCoordinates())
      }
      if (geoType.toLowerCase() === 'multipolygon') {
        // console.log(iGeometry.getCoordinates()[0])
        const paraArr = iGeometry.getCoordinates()[0]
        boundingBox = this.getMinMax(paraArr)
      }
      if (boundingBox) {
        aCenterPoint = [(boundingBox[0] + boundingBox[2]) / 2, (boundingBox[1] + boundingBox[3]) / 2]
      }
      // 获取显示级别
      const oView = oMap.getView()
      const r = oView.getResolutionForExtent(boundingBox, oMap.getSize()) // extent和size
      const nLevel = oView.getZoomForResolution(r)
      // 返回中心点和级别
      return { cp: aCenterPoint, lv: nLevel }
    },

    /**
     * @description 判断点是否在多边形内
     * @param {Array} aLonLat 经纬度坐标[float, float]
     * @param {Array} aPolygon 范围经纬度序列[[ [lon,lat],[lon,lat],...[lon,lat] ]]
     * @return {boolean}}
     */
    isPointInPolygon (aLonLat, aPolygon) {
      const oPt = turf.point(aLonLat)
      const oPoly = turf.polygon(aPolygon)
      console.log(turf.booleanPointInPolygon(oPt, oPoly))
      return turf.booleanPointInPolygon(oPt, oPoly)
    },
    /**
     *  判断一个点是否在多边形内部https://blog.csdn.net/u014529917/article/details/77504779/
     *  @param {Array} point 测试点坐标testPoint
     *  @param {Array} polyPoints 多边形坐标集合 [(x,y),(x,y)...]； (必须是EPSG:3857坐标)
     *  @param {String} sRCS 测试点坐标（测试点的参考坐标系）
     *  返回true为真，false为假
     *  */
    insidePolygon (point, polyPoints, sRCS = 'EPSG:4326') {
      let aPoint = null // 测试点的坐标 'EPSG:3857'
      if (sRCS !== 'EPSG:3857') {
        aPoint = olProj.transform(point, sRCS, 'EPSG:3857') // 坐标转换成'EPSG:3857' ； center point
      } else {
        aPoint = point
      }
      const x = aPoint[0]
      const y = aPoint[1]
      var inside = false
      for (let i = 0, j = polyPoints.length - 1; i < polyPoints.length; j = i++) {
        const xi = polyPoints[i][0]
        const yi = polyPoints[i][1]
        const xj = polyPoints[j][0]
        const yj = polyPoints[j][1]

        var intersect = ((yi > y) !== (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi)
        if (intersect) inside = !inside
      }
      return inside
    },
    /**
     *  判断一个点是否在圆的内部
     *  @param {Array} point  测试点坐标
     *  @param {Array} circle 圆心坐标(必须是EPSG:3857坐标)
     *  @param {float} r 圆半径
     *  @param {String} sRCS 测试点坐标（测试点的参考坐标系）
     *  返回true为真，false为假
     *  */
    pointInsideCircle (point, circle, r, sRCS = 'EPSG:4326') {
      let aPoint = null // 测试点的坐标
      if (r === 0) return false
      if (sRCS !== 'EPSG:3857') {
        aPoint = olProj.transform(point, sRCS, 'EPSG:3857') // 坐标转换成'EPSG:3857' ； center point
      } else {
        aPoint = point
      }
      var dx = circle[0] - aPoint[0]
      var dy = circle[1] - aPoint[1]
      return dx * dx + dy * dy <= r * r
    },
    /**
     * @description 坐标转换
     * @param {array} aCoord 要转换的坐标[(x,y),(x,y)...]
     *@param {String} sSrc 坐标原始坐标系 如：'EPSG:3857'
     * @param {String} sDest 坐标目标坐标系 如：'EPSG:4326'
     * @return 转换后的坐标
     */
    coordArrTransform (aCoord, sSrc, sDest) {
      const iNum = aCoord.length
      const newCoord = [] // 新的坐标数组
      // console.log(aCoord, sSrc, sDest)
      if (sSrc === sDest) return aCoord // 坐标系一致，直接返回
      if (iNum > 0) {
        for (let i = 0; i < iNum; i++) {
          const xCoord = olProj.transform(aCoord[i], sSrc, sDest)
          newCoord.push(xCoord)
        }
        return newCoord
      } else {
        return null
      }
    },
    /**
     * @description 坐标转换
     * @param {array} aCoordinate 要转换的坐标；oMap {Object}地图对象
     * @param {String} sSrc 坐标原始坐标系 如：'EPSG:3857'
     * @param {String} sDest 坐标目标坐标系 如：'EPSG:4326'
     * @return 转换后的坐标
     */
    Coordtransform (aCoordinate, sSrc, sDest) {
      return olProj.transform(aCoordinate, sSrc, sDest) // 坐标转换
    },
    // -- 比例尺和指北针部分
    /**
     * @description 比例尺控件的显示与隐藏
     */
    toggleScale () {
      const xCollection = document.getElementsByClassName('ol-scale-line')
      if (xCollection.length > 0) {
        if (xCollection[0].style.display !== 'none') {
          xCollection[0].style.display = 'none'
        } else {
          xCollection[0].style.display = 'block'
        }
        // xCollection[0].style.left = '8px'
        // xCollection[0].style.bottom = '8px'
      }
    },
    /**
     * @description 比例尺控件位置设置
     * @param {String} iLeft 左
     * @param {String} iBottom 下
     */
    setScaleCoord (iLeft, iBottom) {
      iLeft = iLeft || '8px'
      iBottom = iBottom || '8px'
      const xCollection = document.getElementsByClassName('ol-scale-line')
      if (xCollection.length > 0) {
        xCollection[0].style.left = iLeft // '8px'
        xCollection[0].style.bottom = iBottom // '8px'
      }
    },
    /**
     * @description 添加指南针（div方式）
     * @param {String} iLeft 左
     * @param {String} iBottom 下
     * @param {String} sSize 使用图片大小-- 保留
     */
    addCompass (sRight, sTop, sWidth = '100px', sHigh = '200px') {
      sRight = sRight || '8px'
      sTop = sTop || '8px'
      let myCompass = document.getElementById('myCompass')
      if (myCompass) return
      myCompass = document.createElement('div')
      myCompass.id = 'myCompass'
      myCompass.style.position = 'absolute'
      myCompass.style.right = sRight // '8px'
      myCompass.style.top = sTop // '8px'
      myCompass.style.width = sWidth
      myCompass.style.high = sHigh
      const compassImg = document.createElement('img')
      compassImg.src = '/mapres/images/default/compass.png'
      // compassImg.style.width = iWidth
      // compassImg.style.high = iHigh
      compassImg.style.width = '100%'
      compassImg.style.high = 'auto'
      myCompass.appendChild(compassImg)
      // document.getElementById('map-container').appendChild(myCompass)
      const mapContainer = document.getElementsByClassName('card-map')
      if (mapContainer.length > 0) {
        mapContainer[0].appendChild(myCompass)
      }
    },
    /**
     * @description 删除指南针
     */
    delCompass () {
      const myCompass = document.getElementById('myCompass')
      if (myCompass) {
        myCompass.parentNode.removeChild(myCompass)
      }
    },
    /**
     * @description 修改样式属性(需要重新涉及)
     */
    changeStyleByID (sLayerName, oStyle) {
      this.getLayerByName(sLayerName, this.imap)
    },
    /**
     * @description 缩放线条宽窄样式属性
     */
    scaleStrokewidthByID_OLD (sID, scale, bUpdate, sLayerName, oMap = this.imap) {
      if (this.modOldStyle[sID]) return // 本次选择与之前相同
      this.clearStrokewidthscale(sID, sLayerName, oMap) // 如之前有属性变更，删除
      const oLayer = this.getLayerByName(sLayerName, oMap)
      if (!oLayer) {
        return
      }
      const source = oLayer.getSource()
      const oFeature = source.getFeatureById(sID)
      if (oFeature) {
        const newStyle = oFeature.getStyle().clone()
        const oStroke = newStyle.getStroke()
        const iWidth = oStroke.getWidth()
        if (bUpdate) {
          oStroke.setWidth(iWidth * scale)
        } else {
          oStroke.setWidth(iWidth / scale)
        }
        this.modOldStyle[sID] = oFeature.getStyle()
        newStyle.setStroke(oStroke)
        oFeature.setStyle(newStyle)
      }
    },
    // @description 缩放线条宽窄样式属性
    // @param {String} sID 指定feature的ID
    // @param {int} scale 线宽放大尺寸
    // @param {String} sLayerName 操作图层
    // @param {Object} oMap当前地图对象（可忽略）
    // scaleStrokewidthByID (sID, scale, sLayerName, oMap = this.imap) {
    //   if (this.modOldStyle[sID]) return // 本次选择与之前相同
    //   this.clearStrokewidthscale(null, sLayerName, oMap) // 如之前有属性变更，删除
    //   const oLayer = this.getLayerByName(sLayerName, oMap)
    //   if (!oLayer) {
    //     return
    //   }
    //   const source = oLayer.getSource()
    //   const oFeature = source.getFeatureById(sID)
    //   console.log(source, source.getFeatures())
    //   for (let i = 0; i < source.getFeatures().length; i++) {
    //     console.log(source.getFeatures()[i].get('gid'))
    //   }
    //   if (oFeature) {
    //     // console.log(oFeature, oFeature.getStyle())
    //     const oStyle = oFeature.getStyle()
    //     const newStyle = oStyle.clone()
    //     const oStroke = newStyle.getStroke()
    //     const iWidth = oStroke.getWidth()
    //     oStroke.setWidth(iWidth * scale)
    //     this.modOldStyle[sID] = oFeature.getStyle()
    //     newStyle.setStroke(oStroke)
    //     oFeature.setStyle(newStyle)
    //   }
    // },

    scaleStrokewidthByID (sID, scale, sLayerName, oMap = this.imap) {
      console.log(this.modOldStyle[sID])
      if (this.modOldStyle[sID]) return // 本次选择与之前相同
      this.clearStrokewidthscale(null, sLayerName, oMap) // 如之前有属性变更，删除
      const oLayer = this.getLayerByName(sLayerName, oMap)
      if (!oLayer) {
        return
      }
      const source = oLayer.getSource()
      // 如果时标绘层需要特殊处理--组成图标的是多个feature
      if (sLayerName === 'plotLayer') {
        const plotFeatures = source.getFeatures()
        const iNum = plotFeatures.length
        for (let i = 0; i < iNum; i++) {
          const oFeature = plotFeatures[i]
          if (oFeature.get('gid') === sID) {
            // console.log(oFeature.get('ssw'))
            if (oFeature.get('ssw')) return // 已经存在缩放位且有只 scaleStrokeWidth
            const oStyle = oFeature.getStyle()
            const newStyle = oStyle.clone()
            const oStroke = newStyle.getStroke()
            if (!oStroke) continue // 如果没有线宽属性，跳过本次循环
            const iWidth = oStroke.getWidth()
            oStroke.setWidth(iWidth * scale)
            newStyle.setStroke(oStroke)
            oFeature.setStyle(newStyle)
            oFeature.set('ssw', scale)
          }
        }
        this.modOldStyle[sID] = sID
      } else { // 非标会时一般位单个feature
        const oFeature = source.getFeatureById(sID)
        if (oFeature) {
          // console.log(oFeature, oFeature.getStyle())
          const oStyle = oFeature.getStyle()
          const newStyle = oStyle.clone()
          const oStroke = newStyle.getStroke()
          const iWidth = oStroke.getWidth()
          oStroke.setWidth(iWidth * scale)
          this.modOldStyle[sID] = oFeature.getStyle()
          newStyle.setStroke(oStroke)
          oFeature.setStyle(newStyle)
        }
      }
    },
    /**
     *  @description 还原线条宽窄 -- 添加了全局变量；用来保存当前变换样式的featureid
     * @param {String} sID 指定feature的ID
     * @param {String} sLayerName 操作图层
     * @param {Object} oMap当前地图对象（可忽略）
     */
    clearStrokewidthscale (sID, sLayerName, oMap = this.imap) {
      // 没有变化的样式的feature，退出
      if (sLayerName === 'plotLayer') {
        if (JSON.stringify(this.modOldStyle) === '{}') return
        if (sID === null) {
          const keys = Object.keys(this.modOldStyle)
          sID = keys[0]
        }
        const oLayer = this.getLayerByName(sLayerName, oMap)
        if (!oLayer) {
          return
        }
        const source = oLayer.getSource()
        const plotFeatures = source.getFeatures()
        const iNum = plotFeatures.length
        for (let i = 0; i < iNum; i++) {
          const oFeature = plotFeatures[i]
          if (oFeature.get('gid') === sID) {
            const iScale = oFeature.get('ssw')
            // console.log(iScale)
            if (!iScale) continue // 如果没有缩放值，表明没有缩放过，退出
            const oStyle = oFeature.getStyle()
            const newStyle = oStyle.clone()
            const oStroke = newStyle.getStroke()
            if (!oStroke) continue // 如果没有线宽属性，跳过本次循环
            const iWidth = oStroke.getWidth()
            oStroke.setWidth(iWidth / iScale)
            newStyle.setStroke(oStroke)
            oFeature.setStyle(newStyle)
            oFeature.set('ssw', null)
          }
        }
        // delete this.modOldStyle[sID]
      } else { // 非标绘类型处理
        if (JSON.stringify(this.modOldStyle) === '{}') {
          console.log('没有变更样式的Feature')
          return
        } else {
          if (sID === null) {
            const keys = Object.keys(this.modOldStyle)
            sID = keys[0]
          }
          const oLayer = this.getLayerByName(sLayerName, oMap)
          if (!oLayer) {
            return
          }
          const source = oLayer.getSource()
          const oFeature = source.getFeatureById(sID)
          if (oFeature) {
            oFeature.setStyle(this.modOldStyle[sID])
          }
          // delete this.modOldStyle[sID]
        }
      }
      delete this.modOldStyle[sID]
    },

    /**
     * 计算两点距离;需要添加 import {getDistance} from 'ol/sphere';
     * @param {array} coord1 点坐标1
     * @param {array} coord2 点坐标2
     * @param {string} sRCS1 = 'EPSG:4326' 坐标1的参考坐标系
     * @param {string} sRCS2 = 'EPSG:3857' 坐标2的参考坐标系
     */
    getTwoPointDistance (coord1, coord2, sRCS1 = 'EPSG:4326', sRCS2 = 'EPSG:3857') {
      let pt1 = coord1
      let pt2 = coord2
      if (sRCS1 !== 'EPSG:4326') {
        pt1 = olProj.transform(coord1, sRCS1, 'EPSG:4326') // sSrc, sDest
      }
      if (sRCS2 !== 'EPSG:4326') {
        pt2 = olProj.transform(coord2, sRCS2, 'EPSG:4326') // sSrc, sDest
      }
      return getDistance(pt1, pt2, 6378137) // opt_radius
    },
    /**
     * 计算整个路径两点距离;
     * @param {Array} arrPath 路径坐标序列
     * @param {*} sRCS 坐标的参考坐标系
     * @returns pathLen 整个路径的长度--单位米
     */
    getAllPathDistance (arrPath, sRCS = 'EPSG:4326') {
      const numPt = arrPath.length
      if (numPt < 2) return 0
      let pathLen = 0 // 路径总长-单位米
      let pt1 = null
      if (sRCS !== 'EPSG:4326') {
        pt1 = olProj.transform(arrPath[0], sRCS, 'EPSG:4326')
      } else {
        pt1 = arrPath[0]
      }
      // 第一个位置已经取出
      for (let i = 1; i < numPt; i++) {
        let pt2
        if (sRCS !== 'EPSG:4326') {
          pt2 = olProj.transform(arrPath[i], sRCS, 'EPSG:4326')
        } else {
          pt2 = arrPath[i]
        }
        // Two point distance
        const l = getDistance(pt1, pt2, 6378137)
        pathLen += l
        pt1 = pt2
      }
      return pathLen
    },
    // 测量距离方法2--3857
    getTwoPointDistance1 (coord1, coord2, sRCS1 = 'EPSG:4326', sRCS2 = 'EPSG:3857') {
      let pt1 = coord1
      let pt2 = coord2
      if (sRCS1 !== 'EPSG:3857') {
        pt1 = olProj.transform(coord1, sRCS1, 'EPSG:3857') // sSrc, sDest
      }
      if (sRCS2 !== 'EPSG:3857') {
        pt2 = olProj.transform(coord2, sRCS2, 'EPSG:3857') // sSrc, sDest
      }
      const ptx = pt1[0] - pt2[0]
      const pty = pt1[1] - pt2[1]
      return Math.sqrt(ptx * ptx + pty * pty)
    },
    /**  经纬度移动位置后，返回
     * @param {Array} coord [lonlat]
     * @param {float} x 方向移动距离 米
     * @param {float} y 方向移动距离 米
     * @return 移动后的lonlat
    */
    lonlatMove (coord, x = 0, y = 0, sRCS = 'EPSG:4326') {
      let pt = coord
      if (sRCS !== 'EPSG:3857') {
        pt = olProj.transform(coord, sRCS, 'EPSG:3857') // sSrc, sDest
      }
      const px = pt[0] + x
      const py = pt[1] + y
      // lonlat
      return olProj.transform([px, py], 'EPSG:3857', 'EPSG:4326')
    }
    // 当前方法
  }
}
